<?php
class ControllerBlogComment extends Controller {
	private $error = array();
 
	public function index() {
		$this->load->language('blog/comment');

		$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('blog/comment');
		
		$this->getList();
	} 

	public function insert() {
		$this->load->language('blog/comment');

		$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('blog/comment');
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
			$this->model_blog_comment->addComment($this->request->post);
			
			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';
			
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
						
			$this->redirect($this->url->link('blog/comment', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

		$this->getForm();
	}

	public function update() {
		$this->load->language('blog/comment');

		$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('blog/comment');
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {


			$this->model_blog_comment->editComment($this->request->get['id'], $this->request->post);
			
			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';
			
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
						
			$this->redirect($this->url->link('blog/comment', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

		$this->getForm();
	}
	public function publish() {
		$this->load->language('blog/comment');

		$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('blog/comment');
		
		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $id) {
				$this->model_blog_comment->publishComment($id);
			}
			
			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';
			
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
			
			$this->redirect($this->url->link('blog/comment', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}else if(isset($this->request->get['id']) && intval($this->request->get['id'])>0){
			$this->model_blog_comment->publishComment($this->request->get['id']);
		}

		$this->getList();
	}
	
	public function unpublish() {
		$this->load->language('blog/comment');

		$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('blog/comment');
		
		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $id) {
				$this->model_blog_comment->unPublishComment($id);
			}
			
			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';
			
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
			
			$this->redirect($this->url->link('blog/comment', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}else if(isset($this->request->get['id']) && intval($this->request->get['id'])>0){
			$this->model_blog_comment->unPublishComment($this->request->get['id']);
		}

		$this->getList();
	}

	public function delete() { 
		$this->load->language('blog/comment');

		$this->document->setTitle($this->language->get('heading_title'));
		
		$this->load->model('blog/comment');

		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $review_id) {
				$this->model_blog_comment->deleteComment($review_id);
			}

			$this->session->data['success'] = $this->language->get('text_success');

			$url = '';
			
			if (isset($this->request->get['sort'])) {
				$url .= '&sort=' . $this->request->get['sort'];
			}

			if (isset($this->request->get['order'])) {
				$url .= '&order=' . $this->request->get['order'];
			}

			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
						
			$this->redirect($this->url->link('blog/comment', 'token=' . $this->session->data['token'] . $url, 'SSL'));
		}

		$this->getList();
	}

	private function getList() {
		
		
		$filterTerms = array(
			'filter_title',
			'filter_author_name',
			'filter_rating',
			'filter_approved',
			'filter_published'
		);
		
		foreach($filterTerms as $filterTerm){
			if (isset($this->request->get[$filterTerm])) {
				$$filterTerm = $this->request->get[$filterTerm];
			} else {
				$$filterTerm = null;
			}
		}
		
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'r.date_added';
		}
		
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}
		
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
				
		$url = '';
			
			
		foreach($filterTerms as $filterTerm){
			if (isset($this->request->get[$filterTerm])) {
				$url .= '&'.$filterTerm.'=' . $this->request->get[$filterTerm];
			}
		}	
			
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
		
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

  		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('blog/comment', 'token=' . $this->session->data['token'] . $url, 'SSL'),
      		'separator' => ' :: '
   		);
							
		$this->data['insert'] = $this->url->link('blog/comment/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
		$this->data['delete'] = $this->url->link('blog/comment/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');
		$this->data['publish'] = $this->url->link('blog/comment/publish', 'token=' . $this->session->data['token'] . $url, 'SSL');
		$this->data['unpublish'] = $this->url->link('blog/comment/unpublish', 'token=' . $this->session->data['token'] . $url, 'SSL');		

		$this->data['edit_post_link'] = $this->url->link('blog/post/update', 'token=' . $this->session->data['token'] . $url, 'SSL');	
		
		
		$this->data['comments'] = array();
	
		$data = array(
			'sort'  => $sort,
			'order' => $order,
			'start' => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit' => $this->config->get('config_admin_limit')
		);

		foreach($filterTerms as $filterTerm){
			$data[$filterTerm] = $$filterTerm;
			$this->data[$filterTerm] = $$filterTerm;
		}

		$comment_total = $this->model_blog_comment->getTotalComments($data);
		
		$results = $this->model_blog_comment->getComments($data);

    	foreach ($results as $result) {
			$action = array();
						
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('blog/comment/update', 'token=' . $this->session->data['token'] . '&id=' . $result['id'] . $url, 'SSL')
			);
						
			$this->data['comments'][] = array(
				'id'  		  => $result['id'],
				'title'       => $result['title'],
				'blog_post_id'=> $result['blog_post_id'],
				'author_name' => $result['author_name'],
				'author_email'=> $result['author_email'],
				'rating'      => $result['rating'],
				'approved'    => ((int)$result['approved']==1 ? '<font color="#070">Approved</font>' : '<font color="#d00">Not Approved</font>'),
				'published'   => ((int)$result['published']==1 ? '<font color="#070">Published</font>' : '<font color="#d00">UnPublished</font>'),
				'created'     => date($this->language->get('date_format_short'), strtotime($result['created'])),
				'selected'    => isset($this->request->post['selected']) && in_array($result['id'], $this->request->post['selected']),
				'action'      => $action
			);
		}	
	
		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_no_results'] = $this->language->get('text_no_results');

		$this->data['button_insert'] = $this->language->get('button_insert');
		$this->data['button_delete'] = $this->language->get('button_delete');
		$this->data['button_publish'] = $this->language->get('button_publish');
		$this->data['button_unpublish'] = $this->language->get('button_unpublish');
 
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
		
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$url = '';

		if ($order == 'ASC') {
			$url .= '&order=DESC';
		} else {
			$url .= '&order=ASC';
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
		
		$this->data['sort_product'] = $this->url->link('blog/comment', 'token=' . $this->session->data['token'] . '&sort=pd.name' . $url, 'SSL');
		$this->data['sort_author'] = $this->url->link('blog/comment', 'token=' . $this->session->data['token'] . '&sort=r.author_name' . $url, 'SSL');
		$this->data['sort_rating'] = $this->url->link('blog/comment', 'token=' . $this->session->data['token'] . '&sort=r.rating' . $url, 'SSL');
		$this->data['sort_status'] = $this->url->link('blog/comment', 'token=' . $this->session->data['token'] . '&sort=r.status' . $url, 'SSL');
		$this->data['sort_date_added'] = $this->url->link('blog/comment', 'token=' . $this->session->data['token'] . '&sort=r.date_added' . $url, 'SSL');
		
		$url = '';

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}
												
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		$pagination = new Pagination();
		$pagination->total = $comment_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('blog/comment', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
			
		$this->data['pagination'] = $pagination->render();
		
		$this->data['token'] = $this->session->data['token'];
		$this->data['sort'] = $sort;
		$this->data['order'] = $order;

		$this->template = 'blog/comment_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}

	private function getForm() {
		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_approved'] = $this->language->get('text_approved');
		$this->data['text_approved_not'] = $this->language->get('text_approved_not');
		$this->data['text_none'] = $this->language->get('text_none');
		$this->data['text_select'] = $this->language->get('text_select');

		$this->data['entry_post'] = $this->language->get('entry_post');
		$this->data['entry_author'] = $this->language->get('entry_author');
		$this->data['entry_rating'] = $this->language->get('entry_rating');
		$this->data['entry_approved'] = $this->language->get('entry_approved');
		$this->data['entry_content'] = $this->language->get('entry_content');
		$this->data['entry_good'] = $this->language->get('entry_good');
		$this->data['entry_bad'] = $this->language->get('entry_bad');

		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');

 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
 		
		if (isset($this->error['blog_post_id'])) {
			$this->data['error_blog_post_id'] = $this->error['blog_post_id'];
		} else {
			$this->data['error_blog_post_id'] = '';
		}
		
 		if (isset($this->error['author_name'])) {
			$this->data['error_author'] = $this->error['author_name'];
		} else {
			$this->data['error_author'] = '';
		}
		
 		if (isset($this->error['content'])) {
			$this->data['error_content'] = $this->error['content'];
		} else {
			$this->data['error_content'] = '';
		}

		$url = '';
			
		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}
		
		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}
				
   		$this->data['breadcrumbs'] = array();

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);

   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('blog/comment', 'token=' . $this->session->data['token'] . $url, 'SSL'),
      		'separator' => ' :: '
   		);
										
		if (!isset($this->request->get['id'])) { 
			$this->data['action'] = $this->url->link('blog/comment/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
		} else {
			$this->data['action'] = $this->url->link('blog/comment/update', 'token=' . $this->session->data['token'] . '&id=' . $this->request->get['id'] . $url, 'SSL');
		}
		
		$this->data['cancel'] = $this->url->link('blog/comment', 'token=' . $this->session->data['token'] . $url, 'SSL');

		$this->data['token'] = $this->session->data['token'];

		if (isset($this->request->get['id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
			$comment_info = $this->model_blog_comment->getComment($this->request->get['id']);
		}
			
		$this->load->model('blog/post');

		if (isset($this->request->post['blog_post_id'])) {
			$this->data['blog_post_id'] = $this->request->post['blog_post_id'];
		} elseif (!empty($comment_info)) {
			$this->data['blog_post_id'] = $comment_info['blog_post_id'];
		} else {
			$this->data['blog_post_id'] = '';
		}
		
		if((int)$this->data['blog_post_id']>0){
			$post = $this->model_blog_post->getPostDescriptions($this->data['blog_post_id']);
			$post = array_pop($post);
			$this->data['blog_post'] = $post['title'];
		} else {
			$this->data['blog_post'] = '';
		}
			
		if (isset($this->request->post['author_name'])) {
			$this->data['author_name'] = $this->request->post['author_name'];
		} elseif (!empty($comment_info)) {
			$this->data['author_name'] = $comment_info['author_name'];
		} else {
			$this->data['author_name'] = '';
		}
		
		if (isset($this->request->post['author_email'])) {
			$this->data['author_email'] = $this->request->post['author_email'];
		} elseif (!empty($comment_info)) {
			$this->data['author_email'] = $comment_info['author_email'];
		} else {
			$this->data['author_email'] = '';
		}
		
		if (isset($this->request->post['author_ip'])) {
			$this->data['author_ip'] = $this->request->post['author_ip'];
		} elseif (!empty($comment_info)) {
			$this->data['author_ip'] = $comment_info['author_ip'];
		} else {
			$this->data['author_ip'] = '';
		}
			
		if (isset($this->request->post['rating'])) {
			$this->data['rating'] = $this->request->post['rating'];
		} elseif (!empty($comment_info)) {
			$this->data['rating'] = $comment_info['rating'];
		} else {
			$this->data['rating'] = '';
		}
		
		if (isset($this->request->post['approved'])) {
			$this->data['approved'] = $this->request->post['approved'];
		} elseif (!empty($comment_info)) {
			$this->data['approved'] = $comment_info['approved'];
		} else {
			$this->data['approved'] = '';
		}
		
		if (isset($this->request->post['published'])) {
			$this->data['published'] = $this->request->post['published'];
		} elseif (!empty($comment_info)) {
			$this->data['published'] = $comment_info['published'];
		} else {
			$this->data['published'] = '';
		}

		if (isset($this->request->post['content'])) {
			$this->data['content'] = $this->request->post['content'];
		} elseif (!empty($comment_info)) {
			$this->data['content'] = $comment_info['content'];
		} else {
			$this->data['content'] = '';
		}

		$this->template = 'blog/comment_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}
	
	private function validateForm() {
		if (!$this->user->hasPermission('modify', 'blog/comment')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		if (!$this->request->post['blog_post_id']) {
			$this->error['blog_post_id'] = "Post must be selected to attach this comment.";
		}
		
		if ((utf8_strlen($this->request->post['author_name']) < 3) || (utf8_strlen($this->request->post['author_name']) > 255)) {
			$this->error['author_name'] = $this->language->get('error_author');
		}

		if (utf8_strlen($this->request->post['content']) < 1) {
			$this->error['content'] = $this->language->get('error_content');
		}
		
		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}

	private function validateDelete() {
		if (!$this->user->hasPermission('modify', 'blog/comment')) {
			$this->error['warning'] = $this->language->get('error_permission');
		}

		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}	
}
?>